package com.ct.musicserver.controller;


import com.ct.musicserver.common.errorcode.ControllerErrorCodeConstants;
import com.ct.musicserver.common.exception.ControllerException;
import com.ct.musicserver.common.pojo.CommonResult;
import com.ct.musicserver.common.utils.JacksonUtil;
import com.ct.musicserver.controller.param.DelMusicParam;
import com.ct.musicserver.controller.result.FindMusicResult;
import com.ct.musicserver.service.MusicService;
import com.ct.musicserver.service.dto.FindMusicDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class MusicController {

    @Autowired
    private MusicService musicService;

    @RequestMapping("/music/upload")
    public CommonResult<Boolean> upload(
            @RequestParam String singer,
            @RequestParam("filename") MultipartFile file) {
        log.info("upload UploadMusicParam:{}, {}", singer, file);
        musicService.upload(singer, file);
        return CommonResult.success(Boolean.TRUE, "上传成功!");
    }

    @RequestMapping("/music/get")
    public ResponseEntity<byte[]> get(String path) {
        log.info("get path={}", path);
        ResponseEntity<byte[]> by = musicService.get(path);
        return by;
    }

    @RequestMapping("/music/delete")
    public CommonResult<Boolean> delMusic (@Validated DelMusicParam param) {
        log.info("delMusic DelMusicParam:{}",
                JacksonUtil.writerValueAsString(param));
        musicService.delMusic(param);
        return CommonResult.success(Boolean.TRUE, "删除成功!");
    }

    @RequestMapping("/music/deleteSel")
    public CommonResult<Boolean> delMusicSel(
            @RequestParam("id[]") List<Long> ids) {
        log.info("delMusicSel DelMusicSelParam:{}",
                JacksonUtil.writerValueAsString(ids));
        musicService.delSelById(ids);
        return CommonResult.success(Boolean.TRUE, "批量删除成功!");
    }

    @RequestMapping("/music/findmusic")
    public CommonResult<List<FindMusicResult>> findMusic(String musicName) {
        log.info("findMusic musicName={}", musicName);
        List<FindMusicDTO> findMusicDTOList = musicService.findMusic(musicName);
        return CommonResult.success(convertToFindMusicResultList(findMusicDTOList), "查询到了歌曲的信息");
    }

    private List<FindMusicResult> convertToFindMusicResultList(List<FindMusicDTO> findMusicDTOList) {
        if (null == findMusicDTOList) {
            throw new ControllerException(ControllerErrorCodeConstants.NOT_FIND_MUSIC);
        }
        List<FindMusicResult> findMusicResultList = findMusicDTOList.stream()
                .map(findMusicDTO -> {
                    FindMusicResult findMusicResult = new FindMusicResult();
                    findMusicResult.setId(findMusicDTO.getId());
                    findMusicResult.setSinger(findMusicDTO.getSinger());
                    findMusicResult.setTime(findMusicDTO.getTime());
                    findMusicResult.setTitle(findMusicDTO.getTitle());
                    findMusicResult.setUrl(findMusicDTO.getUrl());
                    findMusicResult.setUserid(findMusicDTO.getUserid());
                    return findMusicResult;
                }).collect(Collectors.toList());
        return findMusicResultList;
    }
}
